package com.turnengine.client.global.game.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import java.io.IOException;

/**
 * The Game Definition Serializer.
 */
public class GameDefinitionSerializer implements IGameDefinitionSerializer {

	@Override
	public IGameDefinition readObject(IDataReader reader) throws IOException {

		// Read fields
		IGame game = new GameSerializer().readObject(reader);
		IGameVersion version = new GameVersionSerializer().readObject(reader);
		IGameInstance instance = new GameInstanceSerializer().readObject(reader);
		IGameHost host = new GameHostSerializer().readObject(reader);

		// Create object
		IGameDefinition object = new GameDefinition();
		object.setGame(game);
		object.setVersion(version);
		object.setInstance(instance);
		object.setHost(host);
		return object;
	}

	public void writeObject(IDataWriter writer, IGameDefinition object) throws IOException {

		// Get fields
		IGame game = object.getGame();
		IGameVersion version = object.getVersion();
		IGameInstance instance = object.getInstance();
		IGameHost host = object.getHost();

		// Write fields
		new GameSerializer().writeObject(writer, game);
		new GameVersionSerializer().writeObject(writer, version);
		new GameInstanceSerializer().writeObject(writer, instance);
		new GameHostSerializer().writeObject(writer, host);
	}
}
